<template>
  <div class="login-container">
    <div class="login-form-wrapper">
      <h2 class="login-title">用户登录</h2>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" label-width="80px">
        
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名" />
        </el-form-item>
        
        <el-form-item label="密码" prop="pwd">
          <el-input v-model="loginForm.pwd" type="password" placeholder="请输入密码" />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="handleLogin" :loading="loading" style="width: 100%">登录</el-button>
        </el-form-item>
        
        <el-form-item>
          <el-button type="default" @click="quickLogin" style="width: 100%">直接登录</el-button>
        </el-form-item>
        
        <div class="login-footer">
          <span>还没有账号？</span>
          <a href="#/register" class="register-link">立即注册</a>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { reactive, ref, onMounted } from 'vue'
import { useUserStore } from '../stores'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

export default {
  name: 'LoginView',
  setup() {
    const loginFormRef = ref(null)
    const loading = ref(false)
    const userStore = useUserStore()
    const router = useRouter()
    
    // 默认填充用户名和密码
    const loginForm = reactive({
      username: '23002973', // 这里可以替换为实际的学号
      pwd: '123456'
    })
    
    // 表单验证规则
    const loginRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' }
      ],
      pwd: [
        { required: true, message: '请输入密码', trigger: 'blur' }
      ]
    }
    
    // 处理登录
    const handleLogin = async () => {
      try {
        await loginFormRef.value.validate()
        
        loading.value = true
        
        // 调用登录接口
        const result = await userStore.login(loginForm)
        
        if (result.success) {
          ElMessage.success('登录成功！')
          // 登录成功后跳转到首页
          setTimeout(() => {
            router.push('/')
          }, 1000)
        } else {
          ElMessage.error(result.message || '登录失败')
        }
      } catch (error) {
        console.error('登录失败:', error)
        if (error.message !== 'cancel') {
          ElMessage.error('登录失败，请检查用户名和密码')
        }
      } finally {
        loading.value = false
      }
    }
    
    // 直接登录快捷按钮
    const quickLogin = () => {
      // 使用当前表单中的默认值进行登录
      handleLogin()
    }
    
    // 组件挂载时检查是否已登录
    onMounted(() => {
      if (userStore.isAuthenticated) {
        ElMessage.success('您已登录')
        window.location.href = '/#/'
      }
    })
    
    return {
      loginFormRef,
      loginForm,
      loginRules,
      loading,
      handleLogin,
      quickLogin
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f0f2f5;
}

.login-form-wrapper {
  background-color: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

.login-title {
  text-align: center;
  margin-bottom: 1.5rem;
  color: #303133;
}

.el-form {
  width: 100%;
}

.el-form-item {
  margin-bottom: 1rem;
}

.el-button {
  margin-bottom: 0.5rem;
}

.login-footer {
  text-align: center;
  margin-top: 1rem;
  color: #606266;
}

.register-link {
  color: #409eff;
  text-decoration: none;
  margin-left: 0.5rem;
}

.register-link:hover {
  color: #66b1ff;
}
</style>